//
//  STAdmobBannerViewController.swift
//  iPhone
//
//  Created by Toj on 5/25/23.
//

import UIKit
import GoogleMobileAds

// MARK: - 懒加载(属性), 变量, 初始化init
class STAdmobBannerViewController: STBaseViewController {
    
    /// **** 不展示别添加到父View内
    /// 不展示添加到父View上, 而AdView高度与 load高度不一致 会报错
     var _adView: GADBannerView?
}

// MARK: - 生命周期
extension STAdmobBannerViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 代理事件
extension STAdmobBannerViewController: GADBannerViewDelegate {
    func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
        guard let adView = _adView else { return }
        view.addSubview(adView)
        adView.snp.makeConstraints { make in
            make.height.equalTo(100)
            make.left.right.centerY.equalTo(view)
        }
    }
    
    func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
        print("-->toj: error: \(error.localizedDescription)")
    }
}

// MARK: - 初始化设置
private extension STAdmobBannerViewController {
    
    func setupMakeAddSubviews() {
        
    }
    
    func setupMakeLayoutSubviews() {
        
    }
    
    func setupMakeInitialize() {
        let size = CGSize(width: .sr_width, height: 100)
        let adsize = GADAdSizeFromCGSize(CGSize(width: .sr_width, height: 100))
        let adView = GADBannerView(adSize: adsize)
        adView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        adView.rootViewController = self
        adView.load(GADRequest())
        adView.delegate = self
        _adView = adView
    }
}
